import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.Arrays;

public class My extends JFrame implements ActionListener {
    public static int[] a = new int[200];
    private ShellSort thread1;
    private QuickSort thread2;
    private MergeSort thread3;

    private SelectSort thread4;

    public My() {
        JMenuBar menubar = new JMenuBar();
        JMenu menu_file = new JMenu("操作");
        menubar.add(menu_file);
        JMenuItem menu_star = new JMenuItem("开始");
        menu_star.addActionListener(this);
        menu_file.add(menu_star);

        JMenuItem menu_stop = new JMenuItem("停止");
        //menu_stop.addActionListener(this);
        menu_file.add(menu_stop);
        JMenuItem menu_cont = new JMenuItem("继续");
        //menu_cont.addActionListener(this);
        menu_file.add(menu_cont);

        menu_file.addSeparator();
        JMenuItem menu_exit = new JMenuItem("退出");
        menu_exit.addActionListener(this);
        menu_file.add(menu_exit);

        JMenu menu_help = new JMenu("帮助");
        menubar.add(menu_help);
        JMenuItem menu_about = new JMenuItem("about");
        menu_about.addActionListener(this);
        menu_help.add(menu_about);


        JLabel label1 = new JLabel("希尔排序");
        JLabel label2 = new JLabel("快速排序");
        JLabel label3 = new JLabel("归并排序");
        JLabel label4 = new JLabel("选择排序");

        thread1 = new ShellSort(a);
        thread2 = new QuickSort(a);
        thread3 = new MergeSort(a);
        thread4 = new SelectSort(a);

        JPanel mypanel = new JPanel();

        mypanel.setLayout(null);
        mypanel.add(label1);
        mypanel.add(label2);
        mypanel.add(label3);
        mypanel.add(label4);

        mypanel.add(thread1);
        mypanel.add(thread2);
        mypanel.add(thread3);
        mypanel.add(thread4);


        label1.setBounds(100, 8, 60, 12);
        label2.setBounds(300, 8, 60, 12);
        label3.setBounds(460, 8, 60, 12);
        label4.setBounds(640, 8, 60, 12);


        thread1.setBounds(60, 20, 200, 440);
        thread2.setBounds(140, 20, 280, 440);
        thread3.setBounds(260, 20, 320, 440);
        thread4.setBounds(360,20,420,440);

        JFrame myframe = new JFrame("多线程排序");
        Container con = myframe.getContentPane();
        con.add(menubar, "North");
        con.add(mypanel);
        myframe.setBackground(Color.white);
        myframe.setDefaultCloseOperation(EXIT_ON_CLOSE);
        myframe.setLocation(200, 100);
        myframe.setSize(800, 600);
        myframe.setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getActionCommand() == "退出")
            System.exit(0);
        if (e.getActionCommand() == "开始") {
            thread1.st();
            thread2.st();
            thread3.st();
            thread4.st();
        }
        if (e.getActionCommand() == "停止") {
            thread1.sto();
            thread2.sto();
            thread3.sto();
            thread4.sto();
        }
        if (e.getActionCommand() == "about") {
            JOptionPane.showMessageDialog(null, "本程序写于2005年6月", "Message", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    public static void main(String args[]){
        for(int i=0;i<a.length;i++)
            a[i]=(int)(Math.random()*140);
        for(int i=0;i<a.length;i++)
            System.out.print(a[i]+" ");
        new My();
    }
}
class ShellSort extends Canvas implements Runnable{    //maopao
    private int[] b = new int[200];
    Thread shellsort = new Thread(this);
    public ShellSort(int[] a){
        for(int i=0;i<a.length;i++)
            b[i]=a[i];
    }
    public void st(){
        shellsort.start();
    }
    public void sto(){
        shellsort.stop();
    }
    public void run(){
        int n = b.length;
        int gap = n / 2;
        while(gap > 0){
            for(int i=gap; i<n; i++) {
                int tmp = b[i];
                int j = i - gap;
                for (; j >= 0; j -= gap) {
                    if (b[j] > tmp) {
                        b[j + gap] = b[j];
                    } else {
                        break;
                    }
                }
                b[j + gap] = tmp;
            }
            gap /= 2;
            for(int i=0;i<n;i++);
                repaint();
            try{
                shellsort.sleep(10);
            } catch(Exception e){
            }
        }
        System.out.println(Arrays.toString(b));
    }
    public void paint(Graphics g){
        g.setColor(Color.BLACK);
        g.drawLine(20,20,20,422);
        g.fillRect(20,421,150,3);
        int y=20;
        int i;
        for( i=0;i<b.length;i++) {
            int z;
            z=20+b[i];
            g.drawLine(20,y,z,y);
            y=y+2;
        }
    }
}
class QuickSort extends Canvas implements Runnable{
    private int[] arr=new int[200] ;
    Thread quick = new Thread(this);
    public QuickSort(int[] a){
        for(int i=0;i<a.length;i++)
            arr[i]=a[i];
    }
    public void st(){
        quick.start();
    }
    public void sto(){
        quick.stop();
    }
    public void run() {
        quick(0, arr.length-1);
    }
    public void quick(int left, int right){
        if(left > right) return;//递归终止条件
        int div = partition(left, right);//得到基准值下标
        quick(left,div-1);//递归基准值前面的值
        quick(div+1,right);//递归基准值后面的值
        for(int i=0;i<arr.length;i++);
        repaint();
        try{
            quick.sleep(10);
        } catch(Exception e){
        }
    }
    public int partition(int left, int right){
        int tmp = left;
        int key = arr[left];
        while(left < right){
            while(left < right && arr[right] >= key){
                right--;
            }
            while(left < right && arr[left] <= key){
                left++;
            }
            swap(left,right);
        }
        swap(tmp,left);
        return left;//or right
    }
    void swap(int l, int r){
        int t = arr[l];
        arr[l] = arr[r];
        arr[r] = t;
    }
    public void paint(Graphics g){
        g.setColor(Color.BLUE);
        g.drawLine(130,20,130,422);
        g.fillRect(130,421,150,3);
        int y=20;
        int i;
        for(i=0;i<arr.length;i++) {
            int z;
            z=130+arr[i];
            g.drawLine(130,y,z,y);
            y=y+2;
        }
    }
}
class MergeSort extends Canvas implements Runnable{
    private int[] b = new int[200];
    Thread mergesort = new Thread(this);
    public MergeSort(int[] a){
        for(int i=0;i<a.length;i++){
            b[i]=a[i];
        }
    }
    public void st(){
        mergesort.start();
    }
    public void sto(){
        mergesort.stop();
    }
    public void mergeSort1(int left, int right){
        if(left >= right) return;
        int mid = left + (right - left)/2;
        mergeSort1(left,mid);// 前 n/2 排序
        mergeSort1(mid+1,right);// 后 n/2 排序
        merge(left,right,mid);// 整体排序
        for(int i=0;i<b.length;i++);
            repaint();
        try{
            mergesort.sleep(10);
        } catch(Exception e){
        }
    }
    public void merge(int left, int right, int mid){
        int s1 = left;
        int s2 = mid+1;
        int k = 0;
        int[] tmp = new int[right-left+1];
        while(s1 <= mid && s2 <= right){
            if(b[s1] <= b[s2]){
                tmp[k++] = b[s1++];
            }else{
                tmp[k++] = b[s2++];
            }
        }
        while(s1 <= mid){
            tmp[k++] = b[s1++];
        }
        while (s2 <= right){
            tmp[k++] = b[s2++];
        }
        for (int i = 0; i < tmp.length; i++) {
            b[i+left] = tmp[i];
        }
    }
    public void run(){
        mergeSort1(0,b.length-1);
    }
    public void paint(Graphics g){
        g.setColor(Color.RED);
        g.drawLine(180,20,180,422);
        g.fillRect(180,421,150,3);
        int y=20;
        int i;
        for( i=0;i<b.length;i++)
        {
            int z;
            z=180+b[i];
            g.drawLine(180,y,z,y);
            y=y+2;
        }
    }
}
class SelectSort extends Canvas implements Runnable{
    private int[] b=new int[200];
    Thread selectsort=new Thread(this);
    public SelectSort(int[] a){
        for(int i=0;i<a.length;i++){
            b[i]=a[i];
        }
    }
    public void st(){
        selectsort.start();
    }
    public void sto(){
        selectsort.stop();
    }
    public void run(){
        int size=b.length;
        int i,j,small,temp;
        for(i=0;i<size-1;i++)
        {
            small=i;
            for(j=i+1;j<size;j++){
                if(b[j]<b[small])
                    small=j;
            }
            if(small!=i)
            {
                temp=b[i];
                b[i]=b[small];
                b[small]=temp;
            }
            for(int k=0;k<size;k++)
                repaint();
            try{
                selectsort.sleep(10);
            }
            catch(Exception e){
            }
        }
    }
    public void paint(Graphics g){
        g.setColor(Color.black);
        g.drawLine(260,20,260,422);
        g.fillRect(260,421,150,3);
        int y=20;
        int i;
        for(i=0;i<b.length;i++){
            int z;
            z=260+b[i];
            g.drawLine(260,y,z,y);
            y=y+2;
        }
    }
}